﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace SizeboxUI
{
	// Token: 0x020003BC RID: 956
	public class PoseView : MonoBehaviour
	{
		// Token: 0x06001A0C RID: 6668 RVA: 0x000A2AE8 File Offset: 0x000A0CE8
		private void Start()
		{
			this.mainCamera = Camera.main;
			this.control = this.mainCamera.GetComponent<InterfaceControl>();
			this.placement = this.control.GetComponent<EditPlacement>();
			Button[] componentsInChildren = base.GetComponentsInChildren<Button>();
			componentsInChildren[0].onClick.AddListener(delegate()
			{
				this.OnPrevious();
			});
			componentsInChildren[1].onClick.AddListener(delegate()
			{
				this.OnNext();
			});
			componentsInChildren[2].onClick.AddListener(delegate()
			{
				this.OnCustomize();
			});
			this.placeholder = (Resources.Load("UI/Button/ThumbPlaceholder") as GameObject);
			this.grid = base.GetComponentInChildren<GridLayoutGroup>();
			this.poseTarget = Resources.Load<PoseHandle>("UI/Pose/Pose Target");
			RectTransform component = this.grid.GetComponent<RectTransform>();
			int num = (int)component.rect.width / 72;
			int num2 = (int)component.rect.height / 84;
			this.thumbCount = num * num2;
			this.thumbs = new Image[this.thumbCount];
			for (int i = 0; i < this.thumbCount; i++)
			{
				GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.placeholder, this.grid.transform);
				gameObject.name = "Thumbnail " + i;
				this.thumbs[i] = gameObject.GetComponent<Image>();
				this.originalColor = this.thumbs[i].color;
				Button component2 = gameObject.GetComponent<Button>();
				int number = i;
				component2.onClick.AddListener(delegate()
				{
					this.OnElementClick(number);
				});
			}
			this.page = 0;
			this.pageCount = this.control.catalog[this.poseCatalog].Length / this.thumbCount + 1;
			this.LoadPage(this.page);
			this.control.OnSelected += this.OnChangedCharacter;
			this.poseHandle = new PoseHandle[this.handleCount];
			for (int j = 0; j < this.handleCount; j++)
			{
				this.poseHandle[j] = UnityEngine.Object.Instantiate<PoseHandle>(this.poseTarget);
				this.poseHandle[j].gameObject.layer = Layers.auxLayer;
				this.poseHandle[j].transform.GetChild(0).gameObject.layer = Layers.auxLayer;
				this.poseHandle[j].gameObject.SetActive(false);
			}
			base.gameObject.SetActive(false);
		}

		// Token: 0x06001A0D RID: 6669 RVA: 0x000A2D8C File Offset: 0x000A0F8C
		private void OnElementClick(int i)
		{
			if (this.thumbs[i].sprite)
			{
				string name = this.thumbs[i].sprite.name;
				this.control.SetPose(name);
				this.EnablePosingMode(false);
			}
		}

		// Token: 0x06001A0E RID: 6670 RVA: 0x000A2DD6 File Offset: 0x000A0FD6
		private void OnChangedCharacter()
		{
			this.UnparentHandles();
			if (!base.gameObject.activeSelf)
			{
				return;
			}
			if (!this.control.giantess)
			{
				base.gameObject.SetActive(false);
				return;
			}
		}

		// Token: 0x06001A0F RID: 6671 RVA: 0x000A2E14 File Offset: 0x000A1014
		private void UnparentHandles()
		{
			if (this.poseHandle == null)
			{
				return;
			}
			for (int i = 0; i < this.handleCount; i++)
			{
				this.poseHandle[i].transform.SetParent(null);
			}
		}

		// Token: 0x06001A10 RID: 6672 RVA: 0x000A2E58 File Offset: 0x000A1058
		private void PrepareCharacter()
		{
			PoseIK poseIK = this.control.giantess.ik.poseIK;
			float scale = this.control.giantess.Scale;
			Transform transform = this.poseHandle[0].transform;
			this.poseHandle[0].SetEffector(poseIK.body, this.control.giantess.transform, scale);
			this.poseHandle[1].SetEffector(poseIK.leftHand, transform, scale);
			this.poseHandle[2].SetEffector(poseIK.rightHand, transform, scale);
			this.poseHandle[3].SetEffector(poseIK.leftFoot, this.control.giantess.transform, scale);
			this.poseHandle[4].SetEffector(poseIK.rightFoot, this.control.giantess.transform, scale);
			this.poseHandle[5].SetEffector(poseIK.leftShoulder, transform, scale);
			this.poseHandle[6].SetEffector(poseIK.rightShoulder, transform, scale);
			this.poseHandle[7].SetEffector(poseIK.leftThight, transform, scale);
			this.poseHandle[8].SetEffector(poseIK.rightThight, transform, scale);
			this.poseHandle[9].SetBendGoal(poseIK.leftElbow, transform, scale);
			this.poseHandle[10].SetBendGoal(poseIK.rightElbow, transform, scale);
			this.poseHandle[11].SetBendGoal(poseIK.leftKnee, this.control.giantess.transform, scale);
			this.poseHandle[12].SetBendGoal(poseIK.rightKnee, this.control.giantess.transform, scale);
			this.poseHandle[13].SetLookAt(poseIK.head, this.control.giantess.transform, scale);
			this.control.giantess.CreateFingerPosers();
		}

		// Token: 0x06001A11 RID: 6673 RVA: 0x000A302F File Offset: 0x000A122F
		private void OnEnable()
		{
			if (this.placement)
			{
				this.placement.updateHandles = false;
			}
		}

		// Token: 0x06001A12 RID: 6674 RVA: 0x000A3050 File Offset: 0x000A1250
		private void OnDisable()
		{
			if (this.placement)
			{
				this.placement.updateHandles = true;
			}
			if (this.control)
			{
				this.control.UpdateCollider();
				this.control.commandEnabled = true;
			}
			this.DisableHandles();
			this.UnparentHandles();
		}

		// Token: 0x06001A13 RID: 6675 RVA: 0x000A30AC File Offset: 0x000A12AC
		private void DisableHandles()
		{
			if (this.poseHandle == null)
			{
				return;
			}
			for (int i = 0; i < this.handleCount; i++)
			{
				if (this.poseHandle[i])
				{
					this.poseHandle[i].gameObject.SetActive(false);
				}
			}
			if (this.control.giantess != null)
			{
				this.control.giantess.DisplayFingerPosers(false);
			}
		}

		// Token: 0x06001A14 RID: 6676 RVA: 0x000A3128 File Offset: 0x000A1328
		private void Update()
		{
			if (!EventSystem.current.IsPointerOverGameObject() && Input.GetMouseButtonDown(0))
			{
				Ray ray = this.mainCamera.ScreenPointToRay(Input.mousePosition);
				RaycastHit raycastHit;
				if (Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, Layers.auxMask))
				{
					this.placement.gizmo.SetTarget(raycastHit.collider.transform.parent);
				}
			}
		}

		// Token: 0x06001A15 RID: 6677 RVA: 0x000A31A0 File Offset: 0x000A13A0
		private void LoadPage(int pag)
		{
			int num = pag * this.thumbCount;
			for (int i = 0; i < this.thumbCount; i++)
			{
				int num2 = num + i;
				if (num2 < this.control.catalog[this.poseCatalog].Length)
				{
					this.thumbs[i].sprite = this.control.catalog[this.poseCatalog][num2];
					this.thumbs[i].color = Color.white;
					this.thumbs[i].GetComponentInChildren<Text>().text = this.thumbs[i].sprite.name.Split(new char[]
					{
						'.'
					})[0];
				}
				else
				{
					this.thumbs[i].sprite = null;
					this.thumbs[i].color = this.originalColor;
					this.thumbs[i].GetComponentInChildren<Text>().text = string.Empty;
				}
			}
		}

		// Token: 0x06001A16 RID: 6678 RVA: 0x000A3299 File Offset: 0x000A1499
		private void OnNext()
		{
			this.page++;
			if (this.page >= this.pageCount)
			{
				this.page = 0;
			}
			this.LoadPage(this.page);
		}

		// Token: 0x06001A17 RID: 6679 RVA: 0x000A32CD File Offset: 0x000A14CD
		private void OnPrevious()
		{
			this.page--;
			if (this.page < 0)
			{
				this.page = this.pageCount - 1;
			}
			this.LoadPage(this.page);
		}

		// Token: 0x06001A18 RID: 6680 RVA: 0x000A3303 File Offset: 0x000A1503
		private void OnCustomize()
		{
			this.EnablePosingMode(true);
		}

		// Token: 0x06001A19 RID: 6681 RVA: 0x000A330C File Offset: 0x000A150C
		public void EnablePosingMode(bool enableIK)
		{
			if (!this.control.giantess || !this.control.giantess.poseMode)
			{
				enableIK = false;
			}
			this.control.EnablePoseIK(enableIK);
			this.control.commandEnabled = !enableIK;
			if (enableIK)
			{
				this.PrepareCharacter();
			}
			else
			{
				this.DisableHandles();
				this.UnparentHandles();
				if (this.control.giantess != null)
				{
					this.control.giantess.DestroyFingerPosers();
				}
			}
		}

		// Token: 0x04001BF8 RID: 7160
		public CommandView command;

		// Token: 0x04001BF9 RID: 7161
		private GameObject placeholder;

		// Token: 0x04001BFA RID: 7162
		private int thumbCount;

		// Token: 0x04001BFB RID: 7163
		private GridLayoutGroup grid;

		// Token: 0x04001BFC RID: 7164
		private InterfaceControl control;

		// Token: 0x04001BFD RID: 7165
		private Image[] thumbs;

		// Token: 0x04001BFE RID: 7166
		private EditPlacement placement;

		// Token: 0x04001BFF RID: 7167
		private Color originalColor;

		// Token: 0x04001C00 RID: 7168
		private int page;

		// Token: 0x04001C01 RID: 7169
		private int pageCount;

		// Token: 0x04001C02 RID: 7170
		private int poseCatalog = 2;

		// Token: 0x04001C03 RID: 7171
		private int handleCount = 14;

		// Token: 0x04001C04 RID: 7172
		private PoseHandle poseTarget;

		// Token: 0x04001C05 RID: 7173
		private PoseHandle[] poseHandle;

		// Token: 0x04001C06 RID: 7174
		private Camera mainCamera;
	}
}
